<?php echo $this->Form->create('Aluno', array('class' => 'form-horizontal'));?>
<div class="row-fluid">
	<p class="required">Os campos com <em>*</em> são de preenchimento obrigatório.</p>
	<div class="control-group required <?php echo ($this->Form->isFieldError('matricula')) ? 'error' : ''; ?>">
		<?php echo $this->Form->Label('matricula', 'Matrícula', array('class' => 'control-label'));	?>
		<div class="controls">
			<?php echo $this->Form->input('matricula', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span5', 'required' => false, 'error' => false));	?>
			<?php echo $this->Form->error('matricula', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
		</div>
	</div>
	<div class="control-group required <?php echo ($this->Form->isFieldError('nome')) ? 'error' : ''; ?>">
		<?php echo $this->Form->Label('nome', 'Nome', array('class' => 'control-label'));	?>
		<div class="controls">
			<?php echo $this->Form->input('nome', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span11', 'required' => false, 'error' => false));	?>
			<?php echo $this->Form->error('nome', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
		</div>
	</div>
	<div class="control-group required <?php echo ($this->Form->isFieldError('email')) ? 'error' : ''; ?>">
		<?php echo $this->Form->Label('email', 'E-mail', array('class' => 'control-label'));	?>
		<div class="controls">
			<?php echo $this->Form->input('email', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span11', 'required' => false, 'error' => false));	?>
			<?php echo $this->Form->error('email', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
		</div>
	</div>
	<div class="control-group">
		<?php echo $this->Form->Label('telefone', 'Telefone', array('class' => 'control-label'));	?>
		<div class="controls">
			<?php echo $this->Form->input('telefone', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span5', 'required' => false));	?>
		</div>
	</div>
	<div class="control-group required">
		<?php echo $this->Form->Label('rg', 'Nº RG', array('class' => 'control-label'));	?>
		<div class="controls">
			<?php echo $this->Form->input('rg', array('type' => 'text', 'label' => false, 'div' => false, 'class' => 'span5', 'required' => false));	?>
		</div>
	</div>
	<div class="control-group required <?php echo ($this->Form->isFieldError('curso_id')) ? 'error' : ''; ?>">
		<?php echo $this->Form->Label('curso_id', 'Curso', array('class' => 'control-label'));	?>
		<div class="controls">
			<div class="input-append">
				<?php echo $this->Form->input('curso_id', array('options' => $cursos, 'empty' => true, 'label' => false, 'div' => false, 'error' => false));	?> 
				<?php echo $this->Html->link('<i class="icon-plus icon-white"></i>', array('controller' => 'cursos', 'action' => 'add'), array('escape' => false, 'class' => 'btn btn-primary', 'id' => 'btn-add-curso')); ?>
			</div>
			<?php echo $this->Form->error('curso_id', null, array('wrap' => 'span', 'class' => 'help-block')); ?>
		</div>
	</div>
</div>
<?php echo $this->Form->hidden('id');?>
<?php echo $this->Form->end();?>

<div id="modal-form-curso" class="modal hide fade">
	<div class="modal-header">
		<?php echo $this->Form->button('×', array('type' => 'button', 'div' => false, 'class' => 'close', 'data-dismiss' => 'modal')); ?>
		<h4>Cadastrar Novo Curso</h4>
	</div>
	<div class="modal-body">
	</div>
	<div class="modal-footer">
		<?php echo $this->Form->button('<i class="icon-awesome-remove icon-white"></i>&nbsp;<span>' . __('Cancelar') . '</span>', array('div' => false, 'class' => 'btn', 'data-dismiss' => 'modal'));?>		
		<?php echo $this->Form->button('<i class="icon-awesome-save icon-white"></i>&nbsp;<span>' . __('Salvar') . '</span>', array('div' => false, 'class' => 'btn btn-primary', 'id' => 'btn-save-curso')); 	?>
	</div>
</div>

<script type="text/javascript">
	$(document).ready(function () {
		$('#AlunoTelefone').mask('(99)9999-9999');
		$('#AlunoCursoId').select2({placeholder: 'Selecione o curso do aluno...'});
		
		$('#btn-add-curso').click(function(event){
			event.preventDefault();
			$("#modal-form-curso .modal-body").empty();
            $.get($(this).attr('href'), function (data) {
            	$("#modal-form-curso .modal-body").html(data);
            	$("#modal-form-curso").modal();  
            })
            .always(function(){
            	$("#ajax-indicator").hide();
            });
		});

		$('#btn-save-curso').click(function(event){
			event.preventDefault();
			var button = $(this);
			var copyButton = button.clone(true);
			var content =  $("#modal-form-curso .modal-body");
			var form = content.children('form');
			
			$("#ajax-indicator").show();
			button.addClass('disabled').children('span').text('Salvando...').attr({disabled: 'disabled'});
		    $.post(form.attr('action'), form.serialize(), function (response) {
		        if ($(response).find('.error').length) {
		        	content.html(response);
		        	button.replaceWith(copyButton);
		        } else {
		           location.reload();
		        }
		    })
		    .always(function(){
		    	$("#ajax-indicator").hide();
		    });
		});
		
	});
</script>